home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Applications / PICSee Dust 1.01 / Quaternary Source / DialogUtils.c < prev    next >
Text File  |  1995-12-03  |  14KB  |  459 lines

  1. /*
  2.     DialogUtils.c++
  3.     Written by Hiep Dam; one or two routines written by others.
  4.     
  5.     Version history:
  6.         6/6/95:        Added GetRadioBtn
  7.         11/9/95        Removed dependency on Compat.h for info on whether
  8.                     color is available or not - OutlineDefaultBorder()
  9.                     now makes its own call to Gestalt.
  10.         11/13/95    Added DrawDisabledDialog() routine. Now no more need
  11.                     for multiple calls to HiliteControl(), etc.
  12.                     Very convenient!
  13.         11/13/95    Oops. Realized DrawDisabledDialog() doesn't really
  14.                     work well - but I'll keep it here for the time being...
  15.                     Added IsDialogEditText, StatText.
  16. */
  17.  
  18. #ifndef __PALETTES__
  19.     #include <Palettes.h>
  20. #endif
  21. #ifndef __GESTALT__
  22.     #include <Gestalt.h>
  23. #endif
  24.  
  25. #include "DialogUtils.h"
  26.  
  27. // ---------------------------------------------------------------------------
  28.  
  29.  
  30. static GDHandle GetDeviceFromLocalRect(Rect *theRect);
  31.  
  32. GDHandle GetDeviceFromLocalRect(Rect *theRect) {
  33.     Rect temp;
  34.     Point tpLeft, btRight;
  35.  
  36.     tpLeft.h = theRect->left;
  37.     tpLeft.v = theRect->top;
  38.     btRight.h = theRect->right;
  39.     btRight.v = theRect->bottom;
  40.     LocalToGlobal(&tpLeft);
  41.     LocalToGlobal(&btRight);
  42.     
  43.     temp.left = tpLeft.h;
  44.     temp.top = tpLeft.v;
  45.     temp.right = btRight.h;
  46.     temp.bottom = btRight.v;
  47.     
  48.     return(GetMaxDevice(&temp));
  49. } // END GetDeviceFromLocalRect
  50.  
  51. // ---------------------------------------------------------------------------
  52.  
  53. void OutlineDefaultBorder(Rect *border, Boolean useColor, Boolean activate) {
  54.     RGBColor oldForeColor, newForeColor;
  55.     RGBColor backColor;
  56.     Rect theBorder = *border;
  57.     PenState oldPen;
  58.  
  59.     GetPenState(&oldPen);            // preserve current pen state
  60.     PenNormal();                    // reset the pen state
  61.  
  62.     PenSize(3,3);                    // set thicker pen size
  63.       InsetRect(&theBorder, -4, -4);    // inset outside of default item
  64.  
  65.     if (!activate) {
  66.         if (useColor) {
  67.             // OK, do it in color            
  68.             GetBackColor(&backColor);
  69.             GetForeColor(&oldForeColor);
  70.             newForeColor = oldForeColor;
  71.             if (GetGray(GetDeviceFromLocalRect(border), &backColor, &newForeColor))
  72.                 RGBForeColor(&newForeColor);
  73.             else
  74.                 // Gray color not avail, so use gray pattern (ugh!)
  75.                 PenPat((ConstPatternParam)&qd.gray);
  76.         }
  77.         else
  78.             // If no color, just use standard gray pattern
  79.             PenPat((ConstPatternParam)&qd.gray);
  80.     }
  81.  
  82.     FrameRoundRect(&theBorder, 16, 16);    // draw the border
  83.  
  84.     if (!activate && useColor)
  85.         RGBForeColor(&oldForeColor);
  86.  
  87.     SetPenState(&oldPen);                // restore the pen state
  88. } // END OutlineDefaultBorder
  89.  
  90. // ---------------------------------------------------------------------------
  91.  
  92. pascal void DrawDefaultBorder(DialogPtr theDialog, short itemNo) {
  93.     GrafPtr    oldPort;
  94.     short iType;
  95.     Handle h;
  96.     Rect box;
  97.  
  98.     GetPort(&oldPort);                            /* preserve current grafport        */
  99.     SetPort(theDialog);
  100.     GetDialogItem(theDialog, itemNo, &iType, &h, &box);    /* get the default (#1) item's info    */
  101.     OutlineDefaultBorder(&box,
  102.         theDialog->portBits.rowBytes < 0,
  103.         (**(ControlHandle)h).contrlHilite != 255);
  104.     SetPort(oldPort);                            /* restore the current grafport        */
  105. } // END DrawDefaultBorder
  106.  
  107. // ---------------------------------------------------------------------------
  108.  
  109. /*
  110.     Note: assumes rect derived from the user item encompasses the scroll
  111.     bar also; i.e. it doesn't make any adjustments.
  112. */
  113. void DrawDefaultListBorder(DialogPtr theDialog, short itemNo, Boolean active) {
  114.     GrafPtr savePort;
  115.     PenState savePen;
  116.     Rect listRect;
  117.     
  118.     GetPort(&savePort);
  119.     SetPort(theDialog);
  120.     GetPenState(&savePen);
  121.  
  122.     GetDItemRect(theDialog, itemNo, &listRect);
  123.     InsetRect(&listRect, -4, -4);
  124.     if (active)
  125.         PenPat((ConstPatternParam)&qd.black);    // Outline
  126.     else
  127.         PenPat((ConstPatternParam)&qd.white);    // Erase
  128.     PenSize(2, 2);
  129.     FrameRect(&listRect);
  130.     
  131.     SetPenState(&savePen);
  132.     SetPort(savePort);
  133. } // END DrawDefaultListBorder
  134.  
  135. // ---------------------------------------------------------------------------
  136.  
  137. // Doesn't work. Don't use (for now).
  138. void DrawDimmedDialogText(DialogPtr theDialog, short item, Boolean dimIt);
  139.  
  140. void DrawDimmedDialogText(DialogPtr theDialog, short item, Boolean dimIt) {
  141.     GrafPtr savePort;
  142.     Str255 dimStr;
  143.     Rect frame;
  144.     RGBColor oldForeColor, newForeColor;
  145.     RGBColor backColor;
  146.     PenState savePen;
  147.     Boolean isColorPort;
  148.  
  149.     GetPort(&savePort);
  150.     SetPort(theDialog);
  151.     GetPenState(&savePen);
  152.  
  153.     isColorPort = theDialog->portBits.rowBytes < 0;
  154.     GetDialogItemText(GetDItemHdl(theDialog, item), dimStr);
  155.  
  156.     GetDItemRect(theDialog, item, &frame);
  157.     MoveTo(frame.left + 1, frame.bottom - 4);
  158.  
  159.     if (dimIt) {
  160.         if (isColorPort) {
  161.             // OK, do it in color            
  162.             GetBackColor(&backColor);
  163.             GetForeColor(&oldForeColor);
  164.             newForeColor = oldForeColor;
  165.             if (GetGray(GetDeviceFromLocalRect(&frame), &backColor, &newForeColor)) {
  166.                 RGBForeColor(&newForeColor);
  167.                 PenMode(patOr);
  168.             }
  169.             else {
  170.                 // Gray color not avail, so use gray pattern (ugh!)
  171.                 PenPat((ConstPatternParam)&qd.gray);
  172.                 PenMode(patBic);
  173.             }
  174.         }
  175.         else {
  176.             // If no color, just use standard gray pattern
  177.             PenPat((ConstPatternParam)&qd.gray);
  178.             PenMode(patBic);
  179.         }
  180.         DrawString(dimStr);
  181.         PaintRect(&frame);
  182.     }
  183.     else
  184.         DrawString(dimStr);
  185.  
  186.     // Is it an editable text item or static text item?
  187.     if (IsDialogEditText(theDialog, item)) {
  188.         InsetRect(&frame, -3, -3);    // Enlarge by 3 pixels
  189.         PenMode(patCopy);
  190.         // RGBForeColor already set to gray, so just frame it
  191.         FrameRect(&frame);
  192.     }
  193.  
  194.     if (dimIt && isColorPort)
  195.         RGBForeColor(&oldForeColor);
  196.  
  197.     SetPenState(&savePen);
  198.     SetPort(savePort);
  199. } // END DrawDimmedDialogText
  200.  
  201. // ---------------------------------------------------------------------------
  202.  
  203. Boolean IsDialogStaticText(DialogPtr theDialog, short theItem) {
  204.     Handle itemH = nil;
  205.     short itemT; Rect itemR;
  206.  
  207.     GetDialogItem(theDialog, theItem, &itemT, &itemH, &itemR);
  208.  
  209.     return(itemT == statText);
  210. } // END IsDialogStaticText
  211.  
  212. Boolean IsDialogEditText(DialogPtr theDialog, short theItem) {
  213.     Handle itemH = nil;
  214.     short itemT; Rect itemR;
  215.  
  216.     GetDialogItem(theDialog, theItem, &itemT, &itemH, &itemR);
  217.  
  218.     return(itemT == editText);
  219. } // END IsDialogEditText
  220.  
  221. // ---------------------------------------------------------------------------
  222.  
  223. void GetDItemRect(DialogPtr theDialog, short theItem, Rect *theRect) {
  224.     Handle itemH; short itemT;
  225.     GetDialogItem(theDialog, theItem, &itemT, &itemH, theRect);
  226. } // END GetDItemRect
  227.  
  228. // ---------------------------------------------------------------------------
  229.  
  230. void SetUserProc(DialogPtr theDialog, short theItem, ProcPtr theProc) {
  231.     Handle itemH; short itemT; Rect itemR;
  232.     GetDialogItem(theDialog, theItem, &itemT, &itemH, &itemR);
  233.     SetDialogItem(theDialog, theItem, itemT, (Handle)theProc, &itemR);
  234. } // END SetUserProc;
  235.  
  236. // ---------------------------------------------------------------------------
  237.  
  238. Handle GetDItemHdl(DialogPtr theDialog, short theItem) {
  239.     Handle itemH = nil;
  240.     short itemT; Rect itemR;
  241.     GetDialogItem(theDialog, theItem, &itemT, &itemH, &itemR);
  242.     return(itemH);
  243. } // END GetDItemHdl
  244.  
  245. // ---------------------------------------------------------------------------
  246.  
  247. ControlHandle GetCtlHdl(DialogPtr theDialog, short theItem) {
  248.     return((ControlHandle)GetDItemHdl(theDialog, theItem));
  249. } // END GetCtlHdl
  250.  
  251. // ---------------------------------------------------------------------------
  252.  
  253. void FlipCtlValue(DialogPtr theDialog, short theItem) {
  254.     short maxVal = GetControlMaximum((ControlHandle)GetDItemHdl(theDialog, theItem));
  255.     short minVal = GetControlMinimum((ControlHandle)GetDItemHdl(theDialog, theItem));
  256.     short curVal = GetControlValue((ControlHandle)GetDItemHdl(theDialog, theItem));
  257.     if (curVal == minVal)
  258.         SetControlValue((ControlHandle)GetDItemHdl(theDialog, theItem), maxVal);
  259.     else
  260.         SetControlValue((ControlHandle)GetDItemHdl(theDialog, theItem), minVal);
  261. } // END FlipCtlValue
  262.  
  263. // ---------------------------------------------------------------------------
  264.  
  265. void SetDlogCtlValue(DialogPtr theDialog, short theItem, short theValue) {
  266.     SetControlValue(GetCtlHdl(theDialog, theItem), theValue);
  267. } // END SetDlogCtlValue
  268.  
  269. short GetDlogCtlValue(DialogPtr theDialog, short theItem) {
  270.     return GetControlValue(GetCtlHdl(theDialog, theItem));
  271. } // END GetDlogCtlValue
  272.  
  273. // ---------------------------------------------------------------------------
  274.  
  275. void EnableDlogCtl(DialogPtr theDialog, short theItem) {
  276.     HiliteControl(GetCtlHdl(theDialog, theItem), 0);
  277. } // END EnableDlogCtl
  278.  
  279. void DisableDlogCtl(DialogPtr theDialog, short theItem) {
  280.     HiliteControl(GetCtlHdl(theDialog, theItem), 255);
  281. } // END DisableDlogCtl
  282.  
  283. // ---------------------------------------------------------------------------
  284.  
  285. void SelectRadioBtn(DialogPtr theDialog, short selectedBtn, short beginRange, short endRange) {
  286.     short i;
  287.     for (i = beginRange; i <= endRange; i++) {
  288.         SetControlValue((ControlHandle)GetDItemHdl(theDialog, i), 0);
  289.     }
  290.     SetControlValue((ControlHandle)GetDItemHdl(theDialog, selectedBtn), 1);
  291. } // END SelectRadioBtn
  292.  
  293. // ---------------------------------------------------------------------------
  294.  
  295. short GetRadioBtn(DialogPtr theDialog, short beginRange, short endRange)
  296. /*
  297.     Given a range of radio buttons, this routines returns the first
  298.     radio button that's selected (i.e. has a value of 1).
  299.  
  300.     Note: Apple User Interface Guidelines state that in a given
  301.     range of radio buttons, one and only one radio button should
  302.     be selected. If you have no radio button selected (although you
  303.     should have one preselected) this routine will return 0. If you
  304.     have more than 1 radio button selected, this routine will return
  305.     the first radio button in the group.
  306. */
  307. {
  308.     short itemSelected = 0;
  309.     short i;
  310.     
  311.     for (i = beginRange; i <= endRange; i++) {
  312.         if (GetControlValue(GetCtlHdl(theDialog, i))) {
  313.             itemSelected = i;
  314.             break;
  315.         }
  316.     }
  317.     
  318.     return(itemSelected);
  319. } // END GetRadioBtn
  320.  
  321. // ---------------------------------------------------------------------------
  322.  
  323. void PushButton(DialogPtr theDialog, short theItem) {
  324.     PushButtonControl(GetCtlHdl(theDialog, theItem));
  325. } // END PushButton
  326.  
  327. // ---------------------------------------------------------------------------
  328.  
  329. void PushButtonControl(ControlHandle theControl) {
  330.     long dummy;
  331.  
  332.     HiliteControl(theControl, inButton);
  333.     Delay(7, &dummy);
  334.     HiliteControl(theControl, 0);
  335. } // END PushButtonControl
  336.  
  337. // ---------------------------------------------------------------------------
  338.  
  339. void SetDItemText(DialogPtr theDialog, short theItem, Str255 iText) {
  340.     Handle itemH; short itemT; Rect itemR;
  341.     GetDialogItem(theDialog, theItem, &itemT, &itemH, &itemR);
  342.     SetDialogItemText(itemH, iText);
  343. } // END SetDItemText
  344.  
  345. // ---------------------------------------------------------------------------
  346.  
  347. // ChangeDITL.
  348. // Adds a new dialog item list to the dialog.
  349. // origCount is the ditl count before you added any extra ditl items to it.
  350. // It uses this as a reference on how many items to remove (shorten).
  351. // newCount returns the new total number of ditl items.
  352.  
  353. void ChangeDITL(DialogPtr theDialog, short DITLid, short origCount, short *newCount, short eraseItem) {
  354.     Rect eraseRect;
  355.     short shortenCount = CountDITL(theDialog) - origCount;
  356.     Handle curDITL = GetResource('DITL', DITLid);
  357.  
  358.     HLock(curDITL);
  359.  
  360.     if (shortenCount > 0)
  361.         ShortenDITL(theDialog, shortenCount);
  362.  
  363.     if (eraseItem != 0) {
  364.         ShowDItem(theDialog, eraseItem);
  365.         GetDItemRect(theDialog, eraseItem, &eraseRect);
  366.         HideDItem(theDialog, eraseItem);
  367.         SetPort(theDialog);
  368.         EraseRect(&eraseRect);
  369.     }
  370.  
  371.     AppendDITL(theDialog, curDITL, overlayDITL);
  372.     HUnlock(curDITL);
  373.     ReleaseResource(curDITL);
  374.     *newCount = CountDITL(theDialog);
  375. } // END ChangeDITL
  376.  
  377. // ---------------------------------------------------------------------------
  378.  
  379. pascal void FrameBorderBlack(DialogPtr theDialog, short itemNo) {
  380.     GrafPtr savePort;
  381.     Rect frameR;
  382.     PenState savePen;
  383.     
  384.     GetPort(&savePort);
  385.     SetPort(theDialog);
  386.     
  387.     GetPenState(&savePen);
  388.     GetDItemRect(theDialog, itemNo, &frameR);
  389.     PenNormal();
  390.     FrameRect(&frameR);
  391.     
  392.     SetPenState(&savePen);
  393.     SetPort(savePort);
  394. } // END FrameBorderBlack
  395.  
  396. // ---------------------------------------------------------------------------
  397.  
  398. pascal void FrameBorderDotted(DialogPtr theDialog, short itemNo) {
  399.     GrafPtr savePort;
  400.     Rect frameR;
  401.     PenState savePen;
  402.     
  403.     GetPort(&savePort);
  404.     SetPort(theDialog);
  405.     
  406.     GetPenState(&savePen);
  407.     GetDItemRect(theDialog, itemNo, &frameR);
  408.     PenNormal();
  409.     PenPat((ConstPatternParam)&qd.gray);
  410.     FrameRect(&frameR);
  411.     
  412.     SetPenState(&savePen);
  413.     SetPort(savePort);
  414. } // END FrameBorderDotted
  415.  
  416. // -----------------------------------------------------------------------------------
  417.  
  418. // Unlike SetDItemText, which works on text 255 chars or less, this routine
  419. // will set an edit text item to any length of text, if under 32K...
  420.  
  421. void SetDItemBigText(DialogPtr theDialog, short theItem, Ptr theText, short textLen) {
  422.     GrafPtr savePort;
  423.     short curSelection = ((DialogPeek)theDialog)->editField + 1;
  424.     Rect tRect;
  425.     
  426.     GetPort(&savePort);
  427.     SetPort(theDialog);
  428.  
  429.     SelectDialogItemText(theDialog, theItem, 0, 32767);
  430.     TESetText(theText, textLen, ((DialogPeek)theDialog)->textH);
  431.     if (curSelection > 0)
  432.         SelectDialogItemText(theDialog, curSelection, 0, 32767);
  433.     else
  434.         SelectDialogItemText(theDialog, theItem, 0, 32767);
  435.     //DrawDialog(theDialog);
  436.     GetDItemRect(theDialog, theItem, &tRect);
  437.     TEUpdate(&tRect, ((DialogPeek)theDialog)->textH);
  438.  
  439.     SetPort(savePort);
  440. } // END SetDItemBigText
  441.  
  442. // ---------------------------------------------------------------------------
  443.  
  444. /*
  445.     "Grays" out entire port of dialog. Cool!!! (But requires Color QuickDraw)
  446. */
  447. void DrawDisabledDialog(DialogPtr theDialog, RgnHandle disableRgn) {
  448.     GrafPtr savePort;
  449.     RGBColor tint = { 30583, 30583, 30583 };
  450.     RGBColor magnitude = { 30000, 30000, 30000 };
  451.  
  452.     GetPort(&savePort);
  453.     SetPort(theDialog);
  454.     
  455.     PenMode(blend);
  456.     OpColor(&magnitude);
  457.     RGBForeColor(&tint);
  458.     PaintRgn(disableRgn);
  459. } // END DrawDisabledDialog